// Copyright  2004-2008 Backplane Ltd.
//
// This file is part of DOM3XPath
//
// DOM3XPath is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3 of the License, or (at your
// option) any later version.
//
// DOM3XPath is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License
// along with DOM3XPath. If not, see <http://www.gnu.org/licenses/>.
//	
//
//   $Id$
// XPathEvaluator.cpp : Implementation of CXPathEvaluator
#include "stdafx.h"
#include <DOM3XPath.h>
#include "XPathEvaluator.h"
#include "XPathExpression.h"
#include "XPathNSResolver.h"

/////////////////////////////////////////////////////////////////////////////
// CXPathEvaluator

STDMETHODIMP CXPathEvaluator::InterfaceSupportsErrorInfo(REFIID riid)
{
	static const IID* arr[] = 
	{
		&IID_IXPathEvaluator
	};
	for (int i=0; i < sizeof(arr) / sizeof(arr[0]); i++)
	{
		if (::InlineIsEqualGUID(*arr[i],riid))
			return S_OK;
	}
	return S_FALSE;
}


STDMETHODIMP CXPathEvaluator::createExpression(BSTR expression, IXPathNSResolver *resolver, IXPathExpression **retVal)
{
	CComObject<CXPathExpression> * pExpr = NULL;
	CComObject<CXPathExpression>::CreateInstance(&pExpr);
	//CComBSTR(expression).CopyTo(&pExpr->m_sExpr);
	HRESULT hr = pExpr->initialise(expression, resolver);
	if(FAILED(hr))
	{
		retVal = NULL;
		::safeRelease(pExpr);
	}
	else
	{
		hr = pExpr->QueryInterface(IID_IXPathExpression,(void**)retVal);
	}
	return hr;
}

STDMETHODIMP CXPathEvaluator::createNSResolver(IXMLDOMNode *nodeResolver, IXPathNSResolver **retVal)
{
	CComObject<CXPathNSResolver> * pResolver = NULL;
	CComObject<CXPathNSResolver>::CreateInstance(&pResolver);
	HRESULT hr = pResolver->initialise(nodeResolver);
	if(FAILED(hr))
	{
		::safeRelease(pResolver);
		retVal = NULL;
	}
	else
	{
		pResolver->QueryInterface(IID_IXPathNSResolver,(void**)retVal);
	}
	return hr;
}

STDMETHODIMP CXPathEvaluator::evaluate(BSTR expression, IXMLDOMNode *contextNode, IXPathNSResolver *resolver, short type, IXPathResult *result, IXPathResult **retVal)
{
	HRESULT hr;
	IXPathExpression * pExpr = NULL;
	hr = this->createExpression(expression,resolver,&pExpr);
	if(FAILED(hr))
	{
		retVal=NULL;
	}
	else
	{
		hr =  pExpr->evaluate(contextNode,type,result,retVal);
		::safeRelease(pExpr);
	}
	return hr;

}
#ifdef COUNT_EVALUATOR
	unsigned long CXPathEvaluator:: sm_Count = 0;	
#endif

